<template>
  <div ref="warp" class="contentWarpBox">
    <div class="searchInfo">
      <div class="left">
        <el-input v-model="searInfo.keyword" placeholder="角色编号/名称/描述" style="width:142px;" />
        <el-button type="primary" style="marginLeft:3px;" @click="searchInfoMation">搜 索</el-button>
      </div>
      <el-button
        v-if="accreditChildren.createRole"
        class="right"
        type="primary"
        @click="dialogVisible = true,isCreate=true"
      >新增角色</el-button>
    </div>
    <el-table ref="table" :data="tableData" border stripe :height="this.$getTableHeight">
      <el-table-column label="角色编号">
        <template slot-scope="scope">{{ scope.row.id ? scope.row.roleCode :'&nbsp;' }}</template>
      </el-table-column>
      <el-table-column prop="roleName" label="角色名称" />
      <el-table-column prop="description" label="角色描述" show-overflow-tooltip />
      <el-table-column label="操作" width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.id">
            <span
              v-if="accreditChildren.editRole"
              class="_primary"
              @click="editorInfo(scope.row)"
            >编辑</span>
            <span v-if="accreditChildren.delRole" class="_delete" @click="deleteInfo(scope.row)">删除</span>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination clearfix">
      <Pagination
        class="right"
        :total-count="totalCount"
        :page-size="page.pageSize"
        :current-page="page.pageNo"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
    </div>
    <el-dialog
      :title="isCreate ? '新建角色' :'编辑角色'"
      center
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="form1"
        :model="item"
        label-width="100px"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="角色编号：" prop="roleCode">
          <el-input v-model="item.roleCode" :disabled="!isCreate" />
        </el-form-item>
        <el-form-item label="角色名称：" prop="roleName">
          <el-input v-model="item.roleName" />
        </el-form-item>
        <el-form-item label="描述：">
          <el-input
            v-model="item.description"
            type="textarea"
            maxlength="50"
            show-word-limit
            rows="3"
            resize="none"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitVal">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
const item = {
  roleCode: '', // 角色编号
  roleName: '', // 角色名称
  description: '' // 描述
}
import mmUrl from '~/utils/utilUrl';
import Pagination from '~/components/commom/pagination';
import {
  reduceArr,
  createDics,
  _currentChange,
  _sizeChange,
  _changeTable,
  _deleteMethods,
  _searchInfo,
  changeTableHeight
} from '~/utils/util';
import regular from '~/utils/regular.js';
export default {
  components: {
    Pagination
  },
  data() {
    return {
      accreditArr: [], // 是否有新增，修改删除等功能
      accreditChildren: {},
      rules: {
        roleCode: regular.dicCode,
        roleName: regular.dicCode
      },
      changeSelectOr: false,
      loading: true,
      isCreate: true,
      item,
      totalCount: 1,
      dialogVisible: false,
      tableData: [],
      searInfo: {
        keyword: ''
      },
      copysearInfo: {
        keyword: ''
      },
      page: {
        maxResultCount: 0,
        pageNo: 0,
        pageSize: 20,
        skipCount: 0
      }
    }
  },
  mounted() {
    this.getInitTableDate()
    const result = JSON.parse(sessionStorage.getItem('menu'))
    this.accreditArr = reduceArr(result, '/' + this.$route.name)
    this.accreditChildren = createDics(
      this.accreditArr.children,
      'permissionCode'
    )
    
  },
  methods: {
    editorInfo(res) {
      this.isCreate = false
      this.item = JSON.parse(JSON.stringify(res))
      this.dialogVisible = true
    },
    deleteInfo(row) {
      const that = this
      _deleteMethods(mmUrl.role.delete, row, that).then(() => {
        that.searchInfoMation('no')
      })
    },
    searchInfoMation(res) {
      this.copysearInfo = JSON.parse(JSON.stringify(this.searInfo))
      const result = _searchInfo(res, this.page, this.copysearInfo)
      this.changeTableData(result)
    },
    changeTableData(res) {
      const that = this
      _changeTable(mmUrl.role.search, res, that)
    },
    handleSizeChange(val) {
      const result = _sizeChange(val, this.page, this.copysearInfo)
      this.changeTableData(result)
    },
    handleCurrentChange(val) {
      const result = _currentChange(val, this.page, this.copysearInfo)
      this.changeTableData(result)
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs['form1'].resetFields()
    },
    getInitTableDate(res) {
      const data = {}
      data.page = JSON.parse(JSON.stringify(this.page))
      Object.assign(data, this.copysearInfo)
      this.$post({
        url: mmUrl.role.pageload,
        data: data
      }).then(res => {
        this.totalCount = res.items.totalCount
        this.tableData = res.items.items
        this.loading = false
        changeTableHeight(this)
      })
    },
    submitVal() {
      this.$refs['form1'].validate(valid => {
        if (valid) {
          this.$post({
            url: this.isCreate ? mmUrl.role.create : mmUrl.role.editor,
            data: {
              item: this.item
            }
          }).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.dialogVisible = false
            this.item = JSON.parse(JSON.stringify(item))
            this.getInitTableDate()
          })
        }
      })
    }
  }
}
</script>

<style lang='scss' module>
.top_search {
  margin-bottom: 12px;
}
</style>
